import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const routes = [
  {
    path: '/',
    redirect: 'work'
  },
  {
    path: '/hire',
    component: () => import('./views/hire'),
    meta: {title: '首页'},
    children: [
      {
        path: '',
        redirect: 'app'
      },
      {
        path: 'app',
        meta: { title: '我要预约' },
        component: () => import('./views/hire/app'),
        children: [
          {
            path: '',
            redirect: 'find'
          },
          {
            path: 'find',
            name: 'hireFind',
            meta: { title: '发现' },
            component: () => import('./views/hire/app/find'),
          },
          {
            path: 'order',
            name: 'hireOrder',
            meta: { title: '订单' },
            component: () => import('./views/hire/app/order'),
          },
          {
            path: 'user',
            name: 'hireUser',
            meta: { title: '个人中心' },
            component: () => import('./views/hire/app/user'),
          }
        ]
      },
      {
        path: 'login',
        name: 'hireLogin',
        meta: { title: '用户登录' },
        component: () => import('./views/hire/page/login'),
      },
      {
        path: 'invite/:adviserId',
        props: true,
        name: 'hireInvite',
        meta: { title: '邀请顾问' },
        component: () => import('./views/hire/page/invite'),
      },
      {
        path: 'designers',
        name: 'designers',
        meta: { title: '设计师列表' },
        component: () => import('./views/hire/page/designers'),
      },
      {
        path: 'designer/:id',
        props: true,
        name: 'designer',
        meta: { title: '设计师主页' },
        component: () => import('./views/hire/page/designer'),
      },
      {
        path: 'advisers',
        name: 'advisers',
        meta: { title: '顾问圈' },
        component: () => import('./views/hire/page/advisers'),
      },
      {
        path: 'advisers/:id',
        name: 'advisersDetail',
        meta: { title: '顾问详情' },
        component: () => import('./views/hire/page/advisers/detail'),
      },
      {
        path: 'book/:designerId',
        props: true,
        name: 'bookStep',
        meta: { title: '预约设计师' },
        component: () => import('./views/hire/page/book'),
      },
      {
        path: 'bookResult',
        name: 'bookResult',
        meta: { title: '支付结果' },
        component: () => import('./views/hire/page/book/result'),
      },
      {
        path: 'orderDetail/:id',
        props: true,
        name: 'orderDetail',
        meta: { title: '订单详情' },
        component: () => import('./views/hire/page/orderDetail'),
      },
      {
        path: 'orderAccept/:id',
        props: true,
        name: 'orderAccept',
        meta: { title: '接单' },
        component: () => import('./views/hire/page/orderAccept'),
      },
      {
        path: 'worksDetail/image/:id',
        props: true,
        name: 'worksImageDetail',
        meta: { title: '图集作品详情' },
        component: () => import('./views/hire/page/worksDetail/image'),
      },
      {
        path: 'worksDetail/article/:id',
        props: true,
        name: 'worksArticleDetail',
        meta: { title: '文章作品详情' },
        component: () => import('./views/hire/page/worksDetail/article'),
      },
      {
        path: 'post',
        name: 'post',
        meta: { title: '发布需求' },
        component: () => import('./views/hire/page/post'),
      },
      {
        path: 'postResult',
        name: 'postResult',
        meta: { title: '发布成功' },
        component: () => import('./views/hire/page/post/result'),
      },
      {
        path: 'favDesigners',
        name: 'favDesigners',
        meta: { title: '我的关注' },
        component: () => import('./views/hire/page/fav'),
      },
      {
        path: 'money',
        name: 'moneyDetail',
        meta: { title: '资金明细' },
        component: () => import('./views/hire/page/money'),
      },
      {
        path: 'comment/:orderId',
        props: true,
        name: 'orderComment',
        meta: { title: '订单评价' },
        component: () => import('./views/hire/page/comment'),
      },
    ]
  },

  {
    path: '/work',
    component: () => import('./views/work'),
    meta: { title: 'UEGE' },
    children: [
      {
        path: '',
        redirect: 'sign'
      },
      {
        path: 'sign',
        name: 'sign',
        component: () => import('./views/work/page/sign'),
        meta: { title: 'SIGN' },
      },
      {
        path: 'app',
        component: () => import('./views/work/app'),
        meta: { title: 'APP' },
        children: [
          {
            path: '',
            redirect: 'book'
          },
          {
            path: 'book',
            name: 'myBook',
            meta: { title: '我收到的预约' },
            component: () => import('./views/work/app/book'),
          },
          {
            path: 'book/detail/:id',
            props: true,
            name: 'myBookDetail',
            meta: { title: '预约详情' },
            component: () => import('./views/work/app/book/detail'),
          },
          {
            path: 'picture',
            name: 'myPicture',
            meta: { title: '我的图集' },
            component: () => import('./views/work/app/picture'),
          },
          {
            path: 'picture/add',
            name: 'myPictureAdd',
            meta: { title: '添加图集' },
            component: () => import('./views/work/app/picture/add'),
          },
          {
            path: 'picture/empty',
            name: 'myPictureEmpty',
            meta: { title: '图集为空' },
            component: () => import('./views/work/app/picture/empty'),
          },
          {
            path: 'profile',
            name: 'myProfile',
            meta: { title: '个人资料' },
            component: () => import('./views/work/app/profile'),
          },
          {
            path: 'adviser',
            name: 'myAdviser',
            meta: { title: '我的顾问' },
            component: () => import('./views/work/app/adviser'),
          },
          {
            path: 'adviser/add',
            name: 'myAdviserAdd',
            meta: { title: '添加顾问' },
            component: () => import('./views/work/app/adviser/add'),
          },
          {
            path: 'adviser/result/:id',
            props: true,
            name: 'myAdviserResult',
            meta: { title: '添加结果' },
            component: () => import('./views/work/app/adviser/result'),
          },
          {
            path: 'adviser/empty',
            name: 'myAdviserEmpty',
            meta: { title: '暂无顾问' },
            component: () => import('./views/work/app/adviser/empty'),
          },
        ]
      }
    ]
  }
];

// add route path
// routes.forEach(route => {
//   route.path = route.path || '/' + (route.name || '');
// });

const router = new Router({ routes });

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = `约个设计 | ${title}`;
  }
  next();
});

router.afterEach((to, from) => {
  localStorage.OriginalPageUrl = JSON.stringify(window.location.href);
})

export {
  router
};
